var id=$("#id").val();
var name=$("#fileName").attr("fileName");
var table = layui.table;
var layer;
var _itkls = {};
var jsondatas = {
    id:id
}
//替换
layui.use('upload', function() {
    var upload = layui.upload;

});
// //分页
// $(".tcdPageCode").createPage({
//     pageCount: 100,
//     current: 1,
//     backFn: function(p) {
//         //console.log(p);
//     }
// });
// //进度条
// $('.progress-container').initData({
//     height: 13,
//     percent: 75,
//     bgStartColor: '#5775EF',
//     bgEndColor: '#5775EF'
// })
//打开文件
$("#open").click(function (){
    POBrowser.openWindowModeless('/sys/office/openword?ft=1&fid=' + id, 'width=1200px;height=800px;', '{}')
})
//下载
$("#download").click(function(){

    layerAjaxGet("/it/file/jurisdiction", {"id": id}, '', function (d) {
        if(d.flag){
            window.open("/supervisor/projectManager/exportFile?id=" + id);
            // layer.msg(d.msg, {icon: 6});
        }else{
            layer.msg(d.msg, {icon: 5});
        }
    })
})
//检索
$("#searching").click(function (){
    localStorage.setItem("setjspage",'1');
    layer.open({
        type: 2,
        area: ['380px', '250px'],
        fix: false,
        shadeClose: false,
        shade: 0.4,
        title: "检索设置",
        content: '/it/file/searching?id='+id,
        yes: function(){
            alert(8888);
        },
        btn2: function(){
            layer.closeAll();
        },

        zIndex: layer.zIndex, //重点1,
        success: function(layero){
            layer.setTop(layero); //重点2
        }
    });
})
//删除
$("#del").click(function (){
    layer.confirm('确定删除该文件吗？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $.ajax({
            url:'/it/file/deleteFile',
            type: 'post',
            data:{id:id,deleteState:'2'},
            success:function (d){
                if(d.flag){
                    layer.msg(d.msg, {icon: 6});
                    setTimeout(function(){
                        window.parent._showtab('4d3d0208d59f47ac87580c014c0a5da3', '首页', '/it/homePage', function (iframe) {
                            //点开首页的话,刷新最新,最热,和收藏夹:
                            if (iframe) {
                                $(iframe)[0].contentWindow.flushPage();
                            }
                        });
                        window.parent._delTab(id);
                    },1500)
                }else{
                    layer.msg(d.msg, {icon: 5});
                }
            }
        })
    });
})
//预览
function  preview(obj){
    var previewId=$(obj).attr('previewId');
    var b=$(obj).attr('url');
    var fileName = $(obj).prev().text();
    var fileExtension = b.substring(b.lastIndexOf('.') + 1).toLowerCase();
    var fileType=format(fileExtension);
    if(fileType==1){
        var imgSrc = b;
        var w = ($(window).width() *0.8);
        var h = ($(window).height() *0.8);
        imgSrc=imgSrc+"?v="+Math.random();
        getImageWidth(imgSrc,function(imgW,imgH){
            if(imgW*1 > w*1){
                imgW = w*1;
            }
            if(imgH*1 > h*1){
                imgH = h*1;
            }
            layer.open({
                type: 1,
                title: false,
                closeBtn: 1,
                area: [imgW+"px",imgH+"px"],
                skin: 'layui-layer-nobg', //没有背景色
                shadeClose: true,
                content: '<img src="/supervisor/projectManager/img?fid=' + previewId + '" height="'+imgH+'px" width="'+imgW+'px" />'
            });
        })
    }else if(fileType==2){
        layer.open({
            type: 1,
            title: false,
            closeBtn: 1,
            area: ["80%","80%"],
            skin: 'layui-layer-nobg', //没有背景色
            shadeClose: true,
            content: '<video src="'+b+'" controls="controls" style="width: 100%;height: 100%"></video>'
        });
    }else{
        POBrowser.openWindowModeless('/sys/office/openword?ft=3&fid=' + previewId, 'width=1200px;height=800px;', '{}')
    }
}

// 获取图片真实高度
function getImageWidth(url, callback) {
    var img = new Image();
    img.src = url;
    // 如果图片被缓存，则直接返回缓存数据
    if (img.complete) {
        callback(img.width, img.height);
    } else {
        img.onload = function () {
            callback(img.width, img.height);
        }
    }
}
//判断文件类型  图片：1；视频：2；其他类型：0
function format(type){
    if(type=='jpg'||type=='jpeg'||type=='png'||type=='gif'||type=='psd'){
        return 1;
    }else if(type=='mp4'||type=='avi'||type=='wmv'||type=='mpg'||type=='psd'||type=='swf'||type=='mpeg'){
        return 2;
    }else{
        return 0;
    }
}

//移动
$("#move").click(function (){
    localStorage.setItem("movefromfileid",id);
    localStorage.setItem("movefromfilename",name);
    var filetype = "文件";
    localStorage.setItem("movefromzskfiletype",filetype);
    //打开目录树页面：
    var w = ($(window).width()*0.4);
    var h = ($(window).height()*0.8);
    layer.open({
        type: 2,
        title: false,
        closeBtn: 1,
        shade: 0.8,
        area: [w+'px',h+'px'],
        content: '/it/dic/showDicTree?id='+id,
        success: function(layero, index){
            movePage = index;
        },
        end: function () {
            location.reload();
        }
    });
})

//收藏/取消收藏 1:已经收藏；0:还未收藏
$("#collect").click(function(){
    var state =$("#collect").attr('state')
    if(state=='1'){
        layer.confirm('确定要取消收藏吗？', {
            btn: ['确定','关闭'] //按钮
        }, function(){
            $.ajax({
                url:'/it/file/fileCollect',
                type: 'post',
                data:{id:id,state:state},
                success:function (d){
                    if(d.flag){
                        layer.msg(d.msg, {icon: 6});
                        setTimeout(function (){
                            window.location.reload();
                        },1000)
                    }else{
                        layer.msg(d.msg, {icon: 5});
                    }
                }
            })
        });
    }else if(state=='0'){
        $.ajax({
            url:'/it/file/fileCollect',
            type:'post',
            data:{id:id,state:state},
            success:function(d){
                if(d.flag){
                    layer.msg(d.msg, {icon: 6});
                    setTimeout(function (){
                        window.location.reload();
                    },1000)
                }else{
                    layer.msg(d.msg, {icon: 5});
                }
            }
        })
    }
})
//文件回收站中的功能
layui.use(['form', 'laydate', 'layer', 'upload', 'element', 'table'], function () {
    var tableData = {};
    _itkls = {
        content: {
            setFileListTable: function(dicId){
                table.render({
                    id: 'fileList',
                    elem: '#fileList',
                    url: '/it/file/selectDel',
                    skin: 'nob',
                    cols:[[
                        {checkbox: true, fixed: true, width: '5%',align: 'center'},
                        {field: 'fileName', title: '名称', width: '35%',align: 'center'},
                        {field: 'deleteUser', title: '创建人', width: '10%',align: 'center'},
                        {field: 'deleteDate', title: '创建时间', width: '15%', sort: true,align: 'center'},
                        {field: 'text', title: '操作', width: '35%', toolbar:'#toolBar',align: 'center'}
                    ]],
                    page: true,
                    done: function(res,curr,count){
                        tableData = res.data
                    }
                })
                table.on('tool(fileList)', function (obj) {
                    var data = obj.data;
                    if (obj.event === 'del') {
                        var fileId = data.id;
                        layer.confirm('删除不可恢复，确定要彻底删除吗？', {
                            btn: ['确定','取消'] //按钮
                        }, function(){
                            $.ajax({
                                url:'/it/file/inGrainDel',
                                type: 'post',
                                data:{ids:fileId},
                                success:function (d){
                                    if(d.flag){
                                        layer.msg(d.msg, {icon: 6});
                                        _itkls.content.flushTable('fileList');
                                    }else{
                                        layer.msg(d.msg, {icon: 5});
                                    }
                                }
                            })
                        });
                    }else if(obj.event === 'restore'){
                        var fileId = data.id;
                        $.ajax({
                            url: '/it/file/restore',
                            data: {fileId:fileId},
                            type: 'post',
                            dataType: 'json',
                            success: function(d){
                                if(d.flag){
                                    layer.msg(d.msg,{icon:1});
                                    _itkls.content.flushTable('fileList');
                                }else{
                                    layer.msg(d.msg,{icon:2});
                                }
                            },error: function(d){
                                parent.layer.msg(d.msg,{icon:2});
                            }
                        })
                    }
                });

            },
            flushTable: function(tableId){
                table.reload(tableId);
            }
        },
        file:{
            //彻底删除
            thoroughDel: function(){
                var checkFileId = '';
                $.each(tableData,function(i,e){
                    if(e.LAY_CHECKED){
                        checkFileId += e.id + ",";
                    }
                })
                checkFileId=checkFileId.substring(0,checkFileId.length-1);
                if(checkFileId){
                    layer.confirm('删除不可恢复，确定要彻底删除吗？', {
                        btn: ['确定','取消'] //按钮
                    }, function(){
                        $.ajax({
                            url: '/it/file/inGrainDel',
                            data: {ids:checkFileId},
                            type: 'post',
                            dataType: 'json',
                            success: function(d){
                                if(d.flag){
                                    layer.msg(d.msg,{icon:1});
                                    _itkls.content.flushTable('fileList');
                                }else{
                                    layer.msg(d.msg,{icon:2});
                                }
                            },error: function(d){
                                parent.layer.msg(d.msg,{icon:2});
                            }
                        })
                    });
                }else{
                    layer.msg("请至少勾选一条",{icon:2});
                    return ;
                }
            },
            restore:function(){
                var checkFileId = '';
                $.each(tableData,function(i,e){
                    if(e.LAY_CHECKED){
                        checkFileId += e.id + ",";
                    }
                })
                checkFileId=checkFileId.substring(0,checkFileId.length-1);
                if(checkFileId){
                    $.ajax({
                        url: '/it/file/restore',
                        data: {fileId:checkFileId},
                        type: 'post',
                        dataType: 'json',
                        success: function(d){
                            if(d.flag){
                                layer.msg(d.msg,{icon:1});
                                _itkls.content.flushTable('fileList');
                            }else{
                                layer.msg(d.msg,{icon:2});
                            }
                        },error: function(d){
                            parent.layer.msg(d.msg,{icon:2});
                        }
                    })
                }else{
                    layer.msg("请至少勾选一条",{icon:2});
                    return ;
                }
            },
            empty:function (){
                layer.confirm('删除不可恢复，确定要清空回收站吗？', {
                    btn: ['确定','取消'] //按钮
                }, function(){
                    $.ajax({
                        url: '/it/file/empty',
                        data: {},
                        type: 'post',
                        dataType: 'json',
                        success: function(d){
                            if(d.flag){
                                layer.msg(d.msg,{icon:1});
                                _itkls.content.flushTable('fileList');
                            }else{
                                layer.msg(d.msg,{icon:2});
                            }
                        },error: function(d){
                            parent.layer.msg(d.msg,{icon:2});
                        }
                    })
                });

            }
        }
    }

})
